import React from 'react';
import Redirect from 'umi/redirect';
import pathToRegexp from 'path-to-regexp';
import { connect } from 'dva';
import Authorized from '../utils/Authorized';
import { getAuthority } from '../utils/authority';
import Exception403 from '../pages/Exception/403';

function AuthComponent({ children, location, routerData }) {
  const auth = getAuthority();
  const isLogin = auth && auth !== 'guest'; // 判断用户是否已经登录
  const getRouteAuthority = (path, routeData) => {
    let authorities;
    routeData.forEach(route => {
      // match prefix
      if (pathToRegexp(`${route.path}(.*)`).test(path)) {
        authorities = route.authority || authorities;

        // get children authority recursively
        if (route.routes) {
          authorities = getRouteAuthority(path, route.routes) || authorities;
        }
      }
    });
    return authorities;
  };

	if (isLogin) {
		return (
			<Authorized
				authority={getRouteAuthority(location.pathname, routerData)}
				noMatch={<Exception403 />}
			>
				{children}
			</Authorized>
		);
	} else return <Redirect to="/user/login" />;
}

export default connect(({ menu }) => ({
  routerData: menu.routerData,
}))(AuthComponent);
